package tw.com.softleader.reins.security.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tw.com.softleader.reins.security.entity.User;


@Repository
public class UserDao {
	
	@Autowired
	private DataSource dataSource;

	public List<User> findAll() {
		
		List<User> result = new ArrayList<User>();
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			conn = dataSource.getConnection();
			stmt = conn.createStatement();
			
			String sql = "SELECT * FROM RIUSER";
			
			rs = stmt.executeQuery(sql);
			
			User user = null;
			while(rs.next()) {
				user = new User();
				user.setId(rs.getLong("id"));
				user.setAccount(rs.getString("account"));
				user.setName(rs.getString("name"));
				result.add(user);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if( rs != null ) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if( stmt != null ) {
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if( conn != null ) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
		}
		
		
		
		return result;
	}
	
	public User findById(Long id) {
		return null;
	}
	
	public void insert(User user) {
		
	}
	
	public void update(User user) {
		
	}
	
	public void delete(User user) {
		
	}
	
}
